XOR queries of a subarray¶
Time: O(N); Space: O(1); medium
Given the array arr of positive integers and the array queries where queries[i] = [Li, Ri], for each query i compute the XOR of elements from Li to Ri (that is, arr[Li] xor arr[Li+1] xor … xor arr[Ri] ).
Return an array containing the result for the given queries.
Example 1:
Input: arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
Output: [2,7,14,8]
Explanation:
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
Example 2:
Input: arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
Output: [8,0,4,4]
Constraints:
1 <= len(arr) <= 3 * 10^4
1 <= arr[i] <= 10^9
1 <= len(queries) <= 3 * 10^4
len(queries[i]) == 2
0 <= queries[i][0] <= queries[i][1] < len(arr)
Hints:
What is the result of x ^ y ^ x ?
Compute the prefix sum for XOR.
Process the queries with the prefix sum values.
[1]:
class Solution1(object):
"""
Time: O(N)
Space: O(1)
"""
def xorQueries(self, arr, queries):
"""
:type arr: List[int]
:type queries: List[List[int]]
:rtype: List[int]
"""
for i in range(1, len(arr)):
arr[i] ^= arr[i-1]
return [arr[right] ^ arr[left-1] if left else arr[right] for left, right in queries]
[2]:
s = Solution1()
arr = [1,3,4,8]
queries = [[0,1],[1,2],[0,3],[3,3]]
assert s.xorQueries(arr, queries) == [2,7,14,8]
arr = [4,8,2,10]
queries = [[2,3],[1,3],[0,0],[0,3]]
assert s.xorQueries(arr, queries) == [8,0,4,4]